package com.pet_service.result;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 全局统一返回结果类
 *
 */
@Accessors//Lombok注解，启用链式访问器模式，可以链式调用setter方法
@Data
public class Result<T> {

    private Integer code;

    private String message;

    private T data;

    public Result(){}

    // 返回数据
    // 基础构建方法
    protected static <T> Result<T> build(T data) {
        Result<T> result = new Result<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    // 通过状态码和消息构建
    public static <T> Result<T> build(T body, Integer code, String message) {
        Result<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    // 通过枚举构建
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public static<T> Result<T> ok(){
        return build(null, ResultCodeEnum.SUCCESS);
    }

    public static<T> Result<T> ok(T data){
        return build(data, ResultCodeEnum.SUCCESS);
    }


    public static<T> Result<T> ok(int code,String message){
        return build(null,code,message);
    }


    public static<T> Result<T> ok(int code,String message,T data){
        return build(data,code,message);
    }



    public static<T> Result<T> fail(){
        return Result.fail(null);
    }
    public static<T> Result<T> fail(T data){
        return build(data, ResultCodeEnum.FAIL);
    }

    public static<T> Result<T> fail(Integer code,String message){
        return  build(null,code,message);
    }

    public boolean isOk() {
        if(this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()) {
            return true;
        }
        return false;
    }
}

